function Z = gauss2derror(coeffs,data);
% amp = coeffs(2);    % amplitude of 2D gaussian
% sigx = coeffs(5);   % sigma_x
% sigy = coeffs(6);   % sigma_y
% xoff = coeffs(3);   % x-direction position offset of gaussian
% yoff = coeffs(4);   % y-direction position offset of gaussian
% offset = coeffs(1); % overall offset of image
% slopex = coeffs(7); % slope in the x-plane
% slopey = coeffs(8); % slope in the y-plane
% %angle = coeffs(9); % angle of rotation of 2d gaussian


amp = coeffs(2);    % amplitude of 2D gaussian
sigx = coeffs(5);   % sigma_x
sigy = coeffs(6);   % sigma_y
xoff = coeffs(3);     % x-direction position offset of gaussian
yoff = coeffs(4);     % y-direction position offset of gaussian
offset = coeffs(1); % overall offset of image
slopex = coeffs(7);   % slope in the x-plane
slopey = coeffs(8);   % slope in the y-plane
%angle = coeffs(9);  %angle of rotation of 2d gaussian

% Split the data matrix into x and y vectors
x = data(:, 1);
y = data(:, 2);

% Vector of errors
w = data(:, 3);

numbpoints = data(:,4);%matrix in which each entry is the number of points
sqrtnumbpoints = sqrt(numbpoints(1,1)); %sqrt of number of points

% FOR 2D GAUSSIAN WITH EQUALLY WEIGHTED DATA
Z =(offset+slopex*(x-xoff)+slopey*(y-yoff)+amp*exp(-(((x-xoff).^2)/(2*sigx^2))-(((y-yoff).^2)/(2*sigy^2))));

% FOR ROTATED 2D GAUSSIAN WITHOUT SLOPES
%Z =offset+amp*exp(-(((x-xoff).*cosd(angle)+(y-yoff).*sind(angle))/(2*sigx)).^2-((-(x-xoff).*sind(angle)+(y-yoff)*cosd(angle))/(2*sigy)).^2);

% FOR 2D GAUSSIAN WITH WEIGHTED DATA
%Z =(offset+slopex*(x-xoff)+slopey*(y-yoff)+amp*exp(-(((x-xoff).^2)/(2*sigx^2))-(((y-yoff).^2)/(2*sigy^2))))./(w*sqrtnumbpoints); % We're trying to fit z = f(x, y) so compute f(x, y)
